#!/usr/bin/perl
#Programmer: Rory Carmichael
#Usage: generate .png files from a directory of stats files -- unreliable due to API changes/challenges

my ($indir, $outdir) = @ARGV;
mkdir $outdir;
foreach my $file (`ls $indir`) {
	open(IF, "$indir/$file");
	while (my $line = <IF>) {
		chomp $line;
		if($line =~ /^cluster/) {
			if(!defined($header)) {
				$header = $line;
				print $header . "\n";
			}
			my $vline = <IF>;
			my $radar = $header . "\n" . $vline;
			#We are currently only printing and making csvs of the "interesting" plots
			if($vline =~ /\t0\t/) {
				print STDERR `echo '$radar' | ./radar_plot.pl > rtmp.png`;
				$cur_clust = [split(/\s+/, $vline)]->[0];
				print $vline;
			} else {
				undef($cur_clust);
				undef($cur_hist);
				last;
			}
		} else {
			$cur_hist .= $line ."\n";
		}
	}
	close(IF);
	if(defined($cur_clust)) {
			print STDERR `echo '$cur_hist'`;
			print STDERR `echo '$cur_hist' | ./g_plots.pl > gtmp.png`;
			print STDERR `convert -page +0+0 gtmp.png -page +300+0 rtmp.png -background white -transparent-color white -layers merge +repage $outdir/$cur_clust.png`;
	} 
	`rm gtmp.png rtmp.png`;
}
